package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.jdl.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TableShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template12 {

    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("11KEY-1")){
                        String dataPeriod = data.get("dataPeriod").asText();
                        textRun.setText(dataPeriod);
                        break;
                    }
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text异常原因:{}",12,e);
                }
            }
        }
    }

    public void table(XSLFShape shape, JsonNode data) {
        TableShape tableShape = (TableShape)shape;
        int numberOfColumns = tableShape.getNumberOfColumns();
        int numberOfRows = tableShape.getNumberOfRows();

        String jsonKey =null;
        //遍历获取每一个单元格
        for (int col =1 ;col < numberOfColumns; col++){
            for (int row = 1; row<numberOfRows; row++ ){
                TableCell cell = tableShape.getCell(row, col);
                if (cell.getTextParagraphs().size() > 0){
                    XSLFTextParagraph cellParagraphs = (XSLFTextParagraph)cell.getTextParagraphs().get(0);
                    if (cellParagraphs.getTextRuns().size()> 0){
                        cellParagraphs.getTextRuns().get(0).setText(getCellData(data, row, col));
                    }
                }
            }
        }

    }

    private String getCellData(JsonNode data, int row, int col) {
        String jsonKey = "ytd";
        if (col == 1){
            jsonKey = "mtd";
        }
        JsonNode jsonNode = data.get("beens");

        String nodeName;
        switch (row){
            case 2:
                nodeName = "总用户数";
                break;
            case 3:
                nodeName = "wes";
                break;
            case 4:
                nodeName = "分享人数";
                break;
            case 5:
                nodeName = "分享次数";
                break;
            case 7:
                nodeName = "兑换人数";
                break;
            case 8:
                nodeName = "消耗总分";
                break;
            case 9:
                nodeName = "获取总分";
                break;
            default:
                nodeName = "";
                break;
        }
        for (JsonNode node : jsonNode) {
            if (node.get("name").asText().equals(nodeName)){
                return node.get(jsonKey).asText();
            }
        }
        return "";
    }
}
